<?php
namespace app\index\service;

use think\facade\Config;
use app\index\model\UserModel;

class WechatService extends CommonService
{

    private $xcx_appid = '';
    private $wx_appid = '';
    private $wx_appsecret = '';
    private $wx_temp_url = 'https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=';
    private $template_todo = 'Ae1vcRje8nQCMr4LfVyzMAT__Cw4C5OWZSqqUxUckiI';
    private $template_bill = '6Lu38bMSsqvOIOqlkc1nIwME-pKjwKnhKm1vx6-FU_c';

    public function __construct()
    {
        parent::__construct();
        $this->xcx_appid = Config::get('service.wx.xcx_appid');
        $this->wx_appid = Config::get('service.wx.appid');
        $this->wx_appsecret = Config::get('service.wx.appsecret');
    }


    public function send_todo_info($uid, $title, $mess, $mid, $table, $meter=''){
        $user = UserModel::get_info_by_pk($uid);
        if(!$user || $user['deleted_user'] > 0 || $user['status'] == 0){
            return true;
        }

        $openid = $this->get_user_openid($uid);
        if(!$openid){
            return true;
        }

        if(mb_strlen($title) > 20){
            $title = mb_substr($title, 0, 17).'...';
        }
        if(mb_strlen($mess) > 20){
            $mess = mb_substr($mess, 0, 17).'...';
        }
        $message = [
            'touser' => $openid,
            'template_id' => $this->template_todo,
            'miniprogram' => [
                'appid' => $this->xcx_appid,
                'pagepath' => 'pagesShow/'.$table.'/show?id='.$mid
            ],
            'data' => [
                'thing8'=>['value'=>$title],
                'thing3'=>['value'=>$mess],
            ]
        ];
        $this->send_data(json_encode($message));
        return true;
    }

    public function send_bill_info($uid, $mess, $created_name, $created_at, $mid, $table, $meter=''){
        $user = UserModel::get_info_by_pk($uid);
        if(!$user || $user['deleted_user'] > 0 || $user['status'] == 0){
            return true;
        }

        $openid = $this->get_user_openid($uid);
        if(!$openid){
            return true;
        }

        if(mb_strlen($mess) > 20){
            $mess = mb_substr($mess, 0, 17).'...';
        }
        $message = [
            'touser' => $openid,
            'template_id' => $this->template_bill,
            'miniprogram' => [
                'appid' => $this->xcx_appid,
                'pagepath' => 'pagesShow/'.$table.'/show?id='.$mid
            ],
            'data' => [
                'thing18'=>['value'=>$mess],
                'thing8'=>['value'=>$created_name],
                'time12'=>['value'=>$created_at],
            ]
        ];

        $this->send_data(json_encode($message));
        return true;
    }





    public function send_data($data){
        $this->getWechatAccessToken();
        $access_token = $this->getTxtAccessToken();
        $url = $this->wx_temp_url.$access_token;
        $res = curl_post_sub($url,$data);
        return $res;
    }
    public function get_user_openid($uid=0){
        $userinfo = UserModel::where(['id'=>$uid])->find();
        if($userinfo && $userinfo['deleted_user'] == 0 && $userinfo['wx_openid']){
            return $userinfo['wx_openid'];
        }
        return '';
    }
    public function getWechatAccessToken(){
        $acc_url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$this->wx_appid."&secret=".$this->wx_appsecret;
        $acc_info = curl_get_sub($acc_url);

        if(isset($acc_info['access_token'])){
            file_put_contents('wx_access_token.txt',$acc_info['access_token']);
        }
    }
    public function getTxtAccessToken(){
        $file_name = 'wx_access_token.txt';
        $access_token = file_get_contents($file_name);
        return $access_token;
    }

}
